package models

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	CodeServerBusy int64 = 8888
	CodeSuccess    int64 = 999 + iota
	CodeImageValidationErr
	CodeNeedLogin
	CodeUserNotExist
	CodeInvalidPassword
	CodeUserExist

	CodeInvalidParam
	CodeInvalidToken
	CodeIPError
	CodeFileErr
	CodeTwoPasswordNoTrue

	CodeVideoUpErr
	CodeImgUpErr
	CodeNotFindFile
	CodeNewPasswordCopyOld
	CodeVideoNotExist
	CodeNoVideo
	CodeNoCreateComment
	CodeVideoFormatErr

	CodeFriendExist
	MaxUserRole
)

var codeErrorMsg = map[int64]string{
	CodeSuccess:            "success",
	CodeServerBusy:         "服务器异常",
	CodeUserNotExist:       "用户不存在",
	CodeUserExist:          "用户已存在",
	CodeInvalidPassword:    "用户密码错误",
	CodeInvalidParam:       "参数校验错误",
	CodeNeedLogin:          "用户未登录",
	CodeInvalidToken:       "token过期",
	CodeIPError:            "端口被占用",
	CodeImageValidationErr: "验证码错误",
	CodeFileErr:            "文件上传失败",
	CodeTwoPasswordNoTrue:  "两次密码不一致",
	CodeVideoUpErr:         "视频读取失败",
	CodeImgUpErr:           "图片读取失败",
	CodeNotFindFile:        "请求文件不存在",
	CodeNewPasswordCopyOld: "新密码和原密码相同",
	CodeVideoNotExist:      "视频不存在",
	CodeNoVideo:            "没有视频了",
	CodeNoCreateComment:    "现在只支持三级评论",
	CodeVideoFormatErr:     "视频格式只支持mp4",
	CodeFriendExist:        "已经是好友了",
	MaxUserRole:            "创建超过最大数量",
}

// RespData 返回值
type RespData struct {
	Count int64       `json:"count,omitempty"` //数量
	Data  interface{} `json:"data,omitempty"`  //数据
	Msg   interface{} `json:"msg"`             //信息
	Code  int64       `json:"code"`            //状态码
}

func getCodeMsg(code int64) string {
	return codeErrorMsg[code]
}
func (r *RespData) SendSuccess(c *gin.Context) {
	r.Code = CodeSuccess
	r.Msg = getCodeMsg(CodeSuccess)
	c.JSON(http.StatusOK, r)
}
func (r *RespData) SendServerBusy(c *gin.Context) {
	r.Data = nil
	r.Msg = getCodeMsg(CodeServerBusy)
	r.Code = CodeServerBusy
	c.JSON(http.StatusOK, r)
}
func (r *RespData) SendCode(c *gin.Context, code int64) {
	r.Data = nil
	r.Msg = getCodeMsg(code)
	r.Code = code
	c.JSON(http.StatusOK, r)
}

func (r *RespData) SendCodeWithMsg(c *gin.Context, code int64, msg interface{}) {
	r.Msg = msg
	r.Code = code
	c.JSON(http.StatusOK, r)
}
